package cc.telecomdigital.tdstock.activity.groups.more;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.SystemInfo;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Http.H_CODE;
import cc.telecomdigital.tdstock.Http.HttpSuperClassStockBaseActivity;
import cc.telecomdigital.tdstock.Http.Services.HttpAppInfo;
import cc.telecomdigital.tdstock.Http.Services.H_HttpServiceRequest.H_UserClient_HttpRequest_Result;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;

/**
 * 查閱用量
 * @author ouyangzhihui
 *
 */
public class CheckUsageActivity extends HttpSuperClassStockBaseActivity
{
	private static final String TAG = "CheckUsageActivity";
	
	private LinearLayout none_container;
	private LinearLayout usage_container;
	private TextView txt_user_name;
	private TextView txt_user_number;
	private TextView txt_closure_to;
	private TextView txt_amount_used;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		TDStockLog.d(TAG, " onCreate");
		
		setContentView(R.layout.more_check_usage);
		
		none_container = (LinearLayout)findViewById(R.id.none_container);
		usage_container = (LinearLayout)findViewById(R.id.usage_container);
		
		txt_user_name = (TextView) findViewById(R.id.user_name);
		txt_user_number = (TextView) findViewById(R.id.user_number);
		txt_closure_to = (TextView) findViewById(R.id.closure_to);
		txt_amount_used = (TextView) findViewById(R.id.amount_used);
		
		Button _toolBtnReturn = (Button) findViewById(R.id.more_returnbtn);
		_toolBtnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(getApplicationContext(), MoreActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NO_USER_ACTION);
				CheckUsageActivity.this.SwitchForwardActivity(MoreActivity.class, intent);
			}
		});
	}
	
	@Override
	protected void onResume()
	{
		SetExectuHttpRequestOnResume(false);
		
		super.onResume();
		TDStockLog.d(TAG, " onResume");
		String flag = ((ITDLApplication)getApplicationContext()).getIsCheckUsageType();
		
		TDStockLog.d(TAG, "flag="+flag);
		
		/*
	 	0: 無限分鐘. 無需要查閱用量.
		1: 分鐘計劃, 需要查閱用量.
		   當進入賽馬或足球頁面前 (Stock page / Monitor page / More page), 不會出現 alert message  顯示免費試用期限.
		2: 分鐘計劃, 需要查閱用量.
		   當進入賽馬或足球頁面前 (Stock page / Monitor page / More page), 會出現 alert message 顯示免費試用期限.
		*/
//		flag = "2";
		if ("1".equals(flag) || "2".equals(flag)) 
		{
			none_container.setVisibility(View.GONE);
			usage_container.setVisibility(View.VISIBLE);
			
			txt_user_name.setText(((ITDLApplication)getApplicationContext()).getUserName(""));
			
			String account = ((ITDLApplication)getApplicationContext()).getSessionAccount();
			String token = ((ITDLApplication)getApplicationContext()).getSessionToken();
			
			TDStockLog.d(TAG, "account="+account+"&token="+token);
			
			SendHttpRequest();
		}
		else /*if ("0".equals(flag))*/
		{
			usage_container.setVisibility(View.GONE);
			none_container.setVisibility(View.VISIBLE);
			
			if(((ITDLApplication)getApplicationContext()).getDataMode() == CommonDefn.DataMode.Delay)
			{
				txt_user_name.setText(getResources().getString(R.string.trial_delay_version));
			}
		}
	}

	@Override
	protected ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}

	@Override
	protected H_CODE GetDataFromHttpServer()
	{
		return HttpAppInfo.SendCheckUsageRequest(actContext, this);
	}

	@Override
	protected void H_UserClient_HttpRequest_Response_Sub(H_UserClient_HttpRequest_Result iResult)
	{
		if(!iResult.MStatus.equals("0"))
		{
			ShowMessageDialog(iResult.MError+"("+iResult.MStatus+")");
			return;
		}
		
		String[] array = iResult.contentStores.get(0);
		
		String accountId = array[0];
		
		if (accountId != null && accountId.trim().length() > 2)
			accountId = SystemInfo.getFormattedAccountId(getApplicationContext(), accountId);
		
		txt_user_number.setText(accountId);
		txt_closure_to.setText(array[1]);
		txt_amount_used.setText(array[2] + " " + getResources().getString(R.string.more_check_use_t));
	}
}
